import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../class/sum/section.dart';
import '../../commont/button.dart';
import '../../model/model.dart';
import '../../model/model_default.dart';
import '../../tool/assets.dart';
import 'delay_item.dart';

class DelayView extends SectionView {
  ChannelModel ch_select;
  void Function(ChannelModel) onClick;
  void Function(ChannelModel) onCompletion;
  DelayView(
      {super.key,
      required this.ch_select,
      required super.ch_list,
      required super.title,
      required this.onClick,
      required this.onCompletion});

  @override
  State<DelayView> createState() => _DelayViewState();
}

class _DelayViewState extends SectionViewState<DelayView> {
  @override
  Widget buildHeader(BuildContext context) {
    return Container(color: Colors.transparent);
  }

  Widget _btn(String img, int index) {
    return imgButton(
        groundColor: Color_bg_view,
        enable: widget.ch_select.enable,
        img: Image.asset(
          img,
          fit: BoxFit.fill,
          width: 25,
          height: 25,
          color: border_color(globalModel),
        ),
        onClick: () {
          setState(() {
            double v = widget.ch_select.delay.float;
            v += index == 0 ? -0.1 : 0.1;
            v = max(widget.ch_select.min_delay, v);
            if (widget.ch_select.delay_type == 'ms' && v > widget.ch_select.max_delay) {
              widget.ch_select.delay = '8';
            } else if (widget.ch_select.delay_type == 'cm' && v > widget.ch_select.max_delay * 34.3) {
              widget.ch_select.delay = '274';
            } else if (widget.ch_select.delay_type == 'in' && v > widget.ch_select.max_delay * 34.3 * 0.3937) {
              widget.ch_select.delay = '108';
            } else {
              widget.ch_select.delay = v.roundDouble(1).toString();
            }
          });
          widget.onCompletion(widget.ch_select);
        });
  }

  @override
  Widget buildContent(BuildContext context) {
    double screenWidth = MediaQuery.of(context).size.width;
    double w = (screenWidth - 5 * 7 - 20).toDouble() / 8.0;
    return SizedBox(
        height: w * 2,
        child: CustomScrollView(
          physics: const NeverScrollableScrollPhysics(),
          slivers: [
            SliverGrid.extent(
              ///交叉轴最大的尺寸
              maxCrossAxisExtent: w,
              crossAxisSpacing: 5.0,
              mainAxisSpacing: 5.0,
              // childAspectRatio: 5 / 4.0,
              // physics: const NeverScrollableScrollPhysics(),
              children: (widget.ch_list as List<ChannelModel>).map(($0) {
                return DelayItem(ch: $0, onClick: widget.onClick, onChange: widget.onCompletion);
              }).toList(),
            ),

            SliverGrid.extent(
                maxCrossAxisExtent: screenWidth - 20,
                crossAxisSpacing: 0,
                mainAxisSpacing: 0,
                childAspectRatio: (screenWidth - 20) / 2.0,
                children: [Container()]),

            ///滑动
            SliverGrid.extent(
              maxCrossAxisExtent: screenWidth - 20,
              crossAxisSpacing: 0,
              mainAxisSpacing: 0,
              childAspectRatio: (screenWidth - 20) / 40.0,
              children: [
                SizedBox(
                  child: Stack(
                    children: [
                      Positioned(left: 10, bottom: 5, width: 20, height: 20, child: _btn(assets.sum, 0)),
                      Positioned(right: 10, bottom: 5, width: 20, height: 20, child: _btn(assets.add, 1)),
                      Positioned(
                          left: 40,
                          right: 40,
                          bottom: 5,
                          height: 20,
                          child: kSlider(
                            value: widget.ch_select.delay.float,
                            min: widget.ch_select.min_delay,
                            max: widget.ch_select.delay_type == 'ms'
                                ? widget.ch_select.max_delay
                                : widget.ch_select.delay_type == 'cm'
                                    ? (widget.ch_select.max_delay * 34.3).roundDouble(0)
                                    : (widget.ch_select.max_delay * 34.3 * 0.3937).roundDouble(0),
                            enable: widget.ch_select.enable,
                            activeColor: Color_theme,
                            thumbColor: Color_theme,
                            onChanged: (p0) {
                              setState(() {
                                ///保留1位小数
                                double v = ((p0 * 100.0).toInt() / 100.0).roundDouble(1);
                                widget.ch_select.delay = v.toString();
                              });
                              widget.onClick(widget.ch_select);
                            },
                            onChangedForSend: (p0) => widget.onCompletion(widget.ch_select),
                          ))
                    ],
                  ),
                )
              ],
            ),
          ],
        ));
  }
}
